package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.event.EventQueryChange;
import ru.rchervot.sanasar.client.event.HandlerQueryChange;
import ru.rchervot.sanasar.client.model.FactoryQuery;
import ru.rchervot.sanasar.client.model.Query;
import ru.rchervot.sanasar.client.model.ResultsPage;
import ru.rchervot.sanasar.client.model.SearchResultsItem;
import ru.rchervot.sanasar.client.rpc.GetPage;
import ru.rchervot.sanasar.client.rpc.GotPage;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;
import ru.rchervot.sanasar.client.rpc.SaveQuery;
import ru.rchervot.sanasar.client.rpc.SavedQuery;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;

public abstract class AbstractFiltersPanelPresenter implements Presenter {

	public interface Display extends Presenter.Display {
  	public HasClickHandlers getSearchButton();
		public HasClickHandlers getCloseButton();
		public HasChangeHandlers getAddFilterListBox();
		public void renderQuery(Query query);
		public String getSelectedFilterName();
  }
	
	private final Display display;
	protected final HandlerManager eventBus;
	protected final RPCServiceAsync service;
	protected final FactoryQuery factoryQuery;
  private Query currentQuery;
	protected abstract GwtEvent<?> getFiltersHideEvent();

	public AbstractFiltersPanelPresenter(Display display, HandlerManager eventBus,
			RPCServiceAsync service, FactoryQuery factoryQuery) {
		this.display = display;
		this.eventBus = eventBus;
		this.service = service;
		this.factoryQuery = factoryQuery;
		
		listenForQueryChange();
	}

	@Override
	public void bindDisplay() {
		display.setPresenter(this);

		display.getCloseButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(getFiltersHideEvent());
			}
		});
		
		display.getSearchButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(getFiltersHideEvent());
								
				if (currentQuery.isSaved()) {
					executeQuery(currentQuery.getID());
				} else {
					service.execute(new SaveQuery(currentQuery),
							new SavedQuery() {
						@Override
						public void saved(Integer queryId) {
							executeQuery(queryId);
						}
					});
				}
			}
		});
		
		display.getAddFilterListBox().addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				String filterName = display.getSelectedFilterName();
				System.out.println("filterName " + filterName);
				currentQuery.addFilter(factoryQuery.buildFilter(filterName));
				eventBus.fireEvent(EventQueryChange.build(currentQuery));
			}
		});
	}

	private void executeQuery(Integer queryId) {
		service.execute(new GetPage(queryId, 0),
				new GotPage<SearchResultsItem>() {
			public void got(ResultsPage<SearchResultsItem> page) {}
		});
	}
	
	protected void listenForQueryChange() {
		eventBus.addHandler(EventQueryChange.getType(),
				new HandlerQueryChange() {
			@Override
			public void onQueryChange(
					EventQueryChange event) {
				currentQuery = event.getQuery();
				display.renderQuery(currentQuery);
			}
		});
	}


}
